package com.china_goldcard.gridpagemenu;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by jk on 16/10/22.
 */

public class GridAdapter<T extends GridMenuModel> extends BaseAdapter {

    protected Context context;//运行上下文
    protected List<T> listData;//数据集合
    protected LayoutInflater listContainer;//视图容器
    protected int itemViewResource;//自定义项视图源id
    private float itemH;
    private GridMenuDataAdapter dataAdapter;

    /**
     * 构造函数
     * @param context
     * @param data
     * @param resource
     */
    public GridAdapter(Context context, List<T> data,int resource,float itemH) {
        this.context = context;
        this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
        this.itemViewResource = resource;
        this.listData = data;
        this.itemH = itemH;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public GridMenuModel getItem(int index) {
        return listData.get(index);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (dataAdapter!=null){
            return dataAdapter.getView(listData.get(position),convertView,parent);
        }else{
            ViewHolder viewHolder = null;
            if (convertView == null){
                convertView = listContainer.inflate(itemViewResource,parent,false);
                viewHolder = new ViewHolder();
                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.grid_menu_item_icon_view);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.grid_menu_item_text_view);
                viewHolder.relativeLayout = (LinearLayout) convertView.findViewById(R.id.horizontal_page_menu_root_view);
                ViewGroup.LayoutParams layoutParams = convertView.getLayoutParams();
                layoutParams.height = (int)itemH;
                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder) convertView.getTag();
            }
            T model = listData.get(position);
            if (model.isFromNet()){
                Picasso.with(context)
                        .load(listData.get(position)
                                .getImageUrl())
                        .placeholder(model.getPlaceHolder())
                        .into(viewHolder.imageView);
            }else{
                viewHolder.imageView.setImageDrawable(context.getResources().getDrawable(model.getIcon()));
            }
            viewHolder.textView.setText(model.getTitle());
            return convertView;
        }
    }

    public GridMenuDataAdapter getDataAdapter() {
        return dataAdapter;
    }

    public void setDataAdapter(GridMenuDataAdapter dataAdapter) {
        this.dataAdapter = dataAdapter;
    }

    class ViewHolder{

        ImageView imageView;

        TextView textView;

        LinearLayout relativeLayout;
    }
}
